<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/include/inc.jsp"%>
<!DOCTYPE html >
<html>
<head>
<%@ include file="/WEB-INF/view/jQgrid.jsp"%>
</head>
<body style="background-color: white;" class="gray-bg">
	<div class="ibox-content">
		<form class="form-horizontal m-t " id="signupForm" method="post"
			enctype="multipart/form-data">
			<input type="hidden" name="status" id="status" class="form-control"  value="1"/>
			<input type="hidden" name="isPast" value="0"/>
			<c:if test="${not empty demandId }">
			<input type="hidden" name="demandId" id="demandId"  value="${demandId }"/>
			<input type="hidden" name="companyId" id="companyId"  value="${companyId }"/>
			</c:if>
			<c:if test="${ empty demandId }">
			<div class="form-group">
				<label class="col-sm-3 control-label">推荐岗位：</label>
				<div class="col-sm-8">
					<input type="text" name="posts" id="posts" class="form-control" placeholder="请输入推荐岗位"  onclick="Recommend()" readonly="readonly"/>
				    <input type="hidden" name="demandId" id="demandId" class="form-control" />
				    <p class="help-block" style="color: red;">*必填项，点击根据关键词查询确认选择</p>
				</div>
			</div>
			</c:if>
			<div class="form-group">
				<label class="col-sm-3 control-label">企业对接人：</label>
				<div class="col-sm-8 form-inline">
				    <select class="form-control" id="docker" name="docker">
				      <option value="">==请选择==</option>
					</select>
					<button type="button" onclick="opencontact();">刷新</button>
					<p class="help-block" style="color: red;">如新增企业联系人，请刷新后选择</p>
				</div>
			</div>
			<c:if test="${not empty talentsId }">
			<input type="hidden" id="talentsId" name="talentsId" value="${talentsId }"/>
			</c:if>
			<c:if test="${empty talentsId }">
			<div class="form-group">
				<label class="col-sm-3 control-label">推荐人选：</label>
				<div class="col-sm-8">
				    <input type="text" name="resumeName" id="resumeName" class="form-control" placeholder="请输入推荐人选"  onclick="Talents()"/>
					<input type="hidden" class="form-control" id="talentsId" name="talentsId" value="${talentsId }"/>
				</div>
			</div>
			</c:if>
			<div class="form-group">
				<label class="col-sm-3 control-label">推荐理由：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" id="reason" name="reason" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">预计到岗时间：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" id="arrivalTime" name="arrivalTime" onfocus="WdatePicker({minDate:'%y-%M-{%d+1}'})" readonly="readonly"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" id="remark" name="remark" />
				</div>
			</div>
			<div class="form-group" style="text-align: center;">
				<div class="col-sm-12">
					<button class="btn btn-primary" type="button" id="sub">提交</button>
				</div>
			</div>
		</form>
	</div>


	<script>
    $(function () {
        $("#sub").click(function () {
        	//批量验证表单非空
			//调用方式
        	var arr=new Array(
        	  new Array('docker','企业对接人未选择!'),
        	  new Array('reason','推荐理由未填写!'),
        	  new Array('arrivalTime','预计到岗时间未填写!')
        	)
        	  for(var i=0;i<arr.length;i++){
	        	  if($("#"+arr[i][0]).val()==''){
	        		  $("#"+arr[i][0]).tips({
	  					side:3,
	  		            msg:arr[i][1],
	  		            bg:'#AE81FF',
	  		            time:1
	  		        });
	        		  $("#"+arr[i][0]).focus();
	  				return;
	  			}
        	  }
        	var demandId = $("#demandId").val();
        	var talentsId = $("#talentsId").val();
        	if(demandId!='' && talentsId!=''){
        		$.ajax({
        			url:'<%=contextPath%>/talentsRecommend/isExists',
        			data:{
        				'talentsId':talentsId,
        				'demandId':demandId
        			},
        			type : 'post',
        			dataType : "json",
        			success : function(data) {
        				if(data.code==200){
        						swal({ 
        							title: data.message+"，是否继续推荐?", 
        							type: "warning", 
        							showCancelButton: true, 
        							confirmButtonColor: "#DD6B55", 
        							confirmButtonText: "No", 
        							cancelButtonText: "Yes", 
        							closeOnConfirm: false, 
        							closeOnCancel: false 
        							}, 
        							function(isConfirm) { 
        								if(isConfirm){
        									swal({
        										title : "窗口即将关闭",
        										type : "success",
        										showCancelButton : false,
        										confirmButtonText : "确定",
        										animation : "slide-from-bottom",
        										text: "2秒后自动关闭。", 
        				      	    			timer: 2000, 
        										closeOnConfirm : true
        									}, function() {
        										top.Dialog.close();
        									});
        								}else{
        									submit();
        								}
        							});
        				}else {
        					submit();
						}
        			}
        		});
        	}
		});
    })
    var position = "${position }";
    function Recommend(){
		 var company_id = "${companyId}";
	   	 var diag = new top.Dialog();
		 diag.Drag=true;
		 diag.Title ="请选择推荐岗位";
		 diag.URL = '<%=contextPath %>/demandEnterprise/chooseDemand?position='+position+'&companyId='+company_id;
		 diag.Width = 850;
		 diag.Height = 750;
		 diag.CancelEvent = function(){ //关闭事件
			 var tempid = diag.innerFrame.contentWindow.document.getElementById('tempids').value;
			 var tempname = diag.innerFrame.contentWindow.document.getElementById('tempnames').value;
			if(tempid.length!==0){
					$("#demandId").val(tempid);
					$("#posts").val(tempname);
					$.ajax({
		        		url: '<%=contextPath%>/company/getContactBy?demandId='+tempid,
		    			type : 'post',
		    			dataType : "json",
		    			success : function(data) {
		    				if(data.code==400){
		    					swal({
									title : "即将进入企业联系人",
									type : "info",
									showCancelButton : false,
									confirmButtonText : "确定",
									animation : "slide-from-bottom",
									closeOnConfirm : true
								}, function() {
									openyem("添加公司联系人", "<%=contextPath%>/company/addContactView?demandId="+tempid, 600, 750);
								});
							}
		    			}
		        	});
					opencontact(tempid);
			} 
			diag.close();
		 };
		 diag.show();
	}
    function Talents() {
	   	 var diag = new top.Dialog();
		 diag.Drag=true;
		 diag.Title ="请选择简历";
		 diag.URL = '<%=contextPath %>/resume/chooseResume?position='+position;
		 diag.Width = 850;
		 diag.Height = 750;
		 diag.CancelEvent = function(){ //关闭事件
			 var tempid = diag.innerFrame.contentWindow.document.getElementById('tempids').value;
		     console.log("返回id:"+tempid);
			 var tempname = diag.innerFrame.contentWindow.document.getElementById('tempnames').value;
			 console.log("返回name:"+tempname);
			if(tempid.length!==0){
					$("#talentsId").val(tempid);
					$("#resumeName").val(tempname);
			} 
			diag.close();
		 };
		 diag.show();
	}
    function opencontact(id) {
		if(typeof(id) == "undefined"){
			id = $("#demandId").val();
		}
		if(id!=''){
			$.ajax({
	    		url: '<%=contextPath%>/company/getContactBy?demandId='+id,
				type : 'post',
				dataType : "json",
				success : function(data) {
					var htm='<option value="">==请选择==</option>';
					if(data.code==200){
						var result = data.contacts;
						for (var i = 0; i < result.length; i++) {
							htm +='<option value="'+result[i].contactId+'">'+result[i].resume.trueName+'/'+result[i].resume.cellphone+'</option>';
						}
					}
					$("#docker").html(htm);
				}
	    	});
		}else {
			swal({
    			title:"请选择推荐岗位",
    			text: "2秒后自动关闭。", 
    			timer: 2000, 
			    showConfirmButton: false
    		});
		}
		
	}
    function submit(){
    	$("#signupForm").ajaxSubmit({
            url: '<%=contextPath%>/talentsRecommend/add',
				type : 'post',
				dataType : "json",
				success : function(data) {
					if (data.code == 200) {
						swal({
							title : data.message,
							type : "success",
							showCancelButton : false,
							confirmButtonText : "确定",
							animation : "slide-from-bottom",
							closeOnConfirm : true
						}, function() {
							top.Dialog.close();
						});
					} else {
						swal("错误", data.message, "error");
					}
				}
			});
    }
	</script>
</body>
</html>